package main.java.com.riant.test2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

class test{}
public class ResourceTest {
    public static void main(String[] args) throws IOException {
        System.out.println("路径测试：");
        System.out.println("XXX.class.getResource(\"/\")" + test.class.getResource("/"));
        System.out.println("路径测试：");
        System.out.println("XXX.class.getResource(\"/\")" + test.class.getResource("/"));
        System.out.println("XXX.class.getResource(\"\")" + test.class.getResource(""));
        System.out.println("XXX.class.getClassLoader().getResource(\"\")" + test.class.getClassLoader().getResource(""));
        System.out.println("---------------------------------");
        System.out.println("XXX.class.getResource(\"config.properties\")" + test.class.getResource("config.properties") );
        System.out.println("XXX.class.getResource(\"/config.properties\")" + test.class.getResource("/config.properties") );
        System.out.println("XXX.class.getClassLoader().getResource(\"config.properties\")" + test.class.getClassLoader().getResource("config.properties") );

        System.out.println("输入流测试：");
        InputStream inPkg = test.class.getResourceAsStream("");
        if( inPkg == null ){
            System.out.println("XXX.class.getResourceAsStream(\"\")" + inPkg);
        }else{
            System.out.println("XXX.class.getResourceAsStream(\"\")" + " the read result is:");
            read(inPkg);
        }
        InputStream inRoot = test.class.getResourceAsStream("/");
        if( inRoot == null ){
            System.out.println("XXX.class.getResourceAsStream(\"/\");" + inRoot);
        }else{
            System.out.println("XXX.class.getResourceAsStream(\"/\")" + " the read result is:");
            read(inRoot);
        }
        InputStream inPkgLoader = test.class.getClassLoader().getResourceAsStream("");
        if( inPkgLoader == null ){
            System.out.println( "XXX.class.getClassLoader().getResourceAsStream(\"\")" + inPkgLoader );
        }else {
            System.out.println( "XXX.class.getClassLoader().getResourceAsStream(\"\")" + " the read result is:" );
            read(inPkgLoader);
        }
        System.out.println("-------------------------");
        InputStream inPkg1 = test.class.getResourceAsStream("config.properties");
        InputStream inRoot1 = test.class.getResourceAsStream("/config.properties");
        InputStream inPkgLoader1 = test.class.getClassLoader().getResourceAsStream("config.properties");
        if( inPkg1 == null ){
            System.out.println( "XXX.class.getResourceAsStream(\"config.properties\")" + inPkg1 );
        }else {
            System.out.println( "XXX.class.getResourceAsStream(\"config.properties\")" + " the read result is:" );
            read(inPkg1);
        }
        if( inRoot1 == null ){
            System.out.println( "XXX.class.getResourceAsStream(\"/config.properties\")" + inRoot1 );
        }else{
            System.out.println( "XXX.class.getResourceAsStream(\"/config.properties\")" + " the read result is:");
            read(inRoot1);
        }
        if( inPkgLoader1 == null ){
            System.out.println( "test.class.getClassLoader().getResourceAsStream(\"config.properties\")" + inPkgLoader1 );
        }else{
            System.out.println( "test.class.getClassLoader().getResourceAsStream(\"config.properties\")" + " the read result is:");
            read(inPkgLoader1);
        }

    }

    public static void read(InputStream in) throws IOException {
        byte[] buffer = new byte[1024];
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        for( int len = 0 ; (len = in.read(buffer)) != -1; ){
            out.write(buffer,0,len);
        }
        byte[] bs = out.toByteArray();
        String str = new String(bs,0,bs.length, Charset.forName("utf-8"));
        System.out.println("    " + str);
    }

}
